package cn.iocoder.yudao.module.product.service.conveniencestore;

import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.product.controller.admin.conveniencestore.vo.ConvenienceStorePageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.conveniencestore.vo.ConvenienceStoreSaveReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.conveniencestore.ConvenienceStoreDO;
import cn.iocoder.yudao.module.product.dal.mysql.conveniencestore.ConvenienceStoreMapper;
import cn.iocoder.yudao.module.product.enums.ConvenienceStoreApproveStatusEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mzt.logapi.context.LogRecordContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.CONVENIENCE_STORE_NOT_EXISTS;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.CONVENIENCE_STORE_SUBMIT_FAIL_NOT_DRAFT;

/**
 * 商铺 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class ConvenienceStoreServiceImpl implements ConvenienceStoreService {

    @Resource
    private ConvenienceStoreMapper convenienceStoreMapper;

    @Override
    public Long createConvenienceStore(ConvenienceStoreSaveReqVO createReqVO) {
        // 插入
        ConvenienceStoreDO convenienceStore = BeanUtils.toBean(createReqVO, ConvenienceStoreDO.class);
        String doorHeaderImage = Stream.of(createReqVO.getDoorHeaderImage()).map(item -> String.valueOf(item)).collect(Collectors.joining(","));
        convenienceStore.setDoorHeaderImage(doorHeaderImage);
        convenienceStoreMapper.insert(convenienceStore);
        // 返回
        return convenienceStore.getId();
    }

    @Override
    public void updateConvenienceStore(ConvenienceStoreSaveReqVO updateReqVO) {
        // 校验存在
        validateConvenienceStoreExists(updateReqVO.getId());
        // 更新
        ConvenienceStoreDO updateObj = BeanUtils.toBean(updateReqVO, ConvenienceStoreDO.class);
        String doorHeaderImage = Stream.of(updateReqVO.getDoorHeaderImage()).map(item -> String.valueOf(item)).collect(Collectors.joining(","));
        updateObj.setDoorHeaderImage(doorHeaderImage);
        convenienceStoreMapper.updateById(updateObj);
    }

    @Override
    public void deleteConvenienceStore(Long id) {
        // 校验存在
        validateConvenienceStoreExists(id);
        // 删除
        convenienceStoreMapper.deleteById(id);
    }

    private ConvenienceStoreDO validateConvenienceStoreExists(Long id) {
        ConvenienceStoreDO convenienceStoreDO = convenienceStoreMapper.selectById(id);
        if (convenienceStoreDO == null) {
            throw exception(CONVENIENCE_STORE_NOT_EXISTS);
        }
        return convenienceStoreDO;

    }

    @Override
    public ConvenienceStoreDO getConvenienceStore(Long id) {
        return convenienceStoreMapper.selectById(id);
    }

    @Override
    public String getDoorHeaderImage(Long id) {
        return convenienceStoreMapper.getDoorHeaderImage(id);
    }

    @Override
    public List<ConvenienceStoreDO> getMyConvenienceStoreList(Long userId) {
        return convenienceStoreMapper.selectList(new LambdaQueryWrapperX<ConvenienceStoreDO>().eq(ConvenienceStoreDO::getCreator, userId).eq(ConvenienceStoreDO::getStatus, 0));
    }

    @Override
    public List<Long> getMyConvenienceStoreIdList(Long userId) {
        return convenienceStoreMapper.getMyConvenienceStoreIdList(userId);
    }

    @Override
    public PageResult<ConvenienceStoreDO> getConvenienceStorePage(ConvenienceStorePageReqVO pageReqVO) {
        return convenienceStoreMapper.selectPage(pageReqVO);
    }

    @Override
    public void submitConvenienceStore(Long id, Long loginUserId) {
        // 1. 校验商铺是否在审批
        ConvenienceStoreDO convenienceStoreDO = validateConvenienceStoreExists(id);
        if (ObjUtil.notEqual(convenienceStoreDO.getApproveStatus(), ConvenienceStoreApproveStatusEnum.DRAFT.getStatus())) {
            throw exception(CONVENIENCE_STORE_SUBMIT_FAIL_NOT_DRAFT);
        }
        // 2. 更新状态
        convenienceStoreMapper.updateById(new ConvenienceStoreDO().setId(id).setApproveStatus(ConvenienceStoreApproveStatusEnum.PROCESS.getStatus()));

        // 3. 记录日志
        LogRecordContext.putVariable("convenienceStoreName", convenienceStoreDO.getName());
    }

    @Override
    public List<ConvenienceStoreDO> getTopConvenienceStoreByAreaId(Integer areaId) {
        List<ConvenienceStoreDO> convenienceStoreDOList = convenienceStoreMapper.selectList(new LambdaQueryWrapper<ConvenienceStoreDO>()
                .eq(ConvenienceStoreDO::getAreaId, areaId)
                .eq(ConvenienceStoreDO::getStatus, 0)
                .orderByDesc(ConvenienceStoreDO::getSort)
                .last("limit 3"));
        return convenienceStoreDOList;
    }

}